package com.disgruntledbots;

import org.andengine.engine.handler.IUpdateHandler;
import org.json.JSONObject;

public class MessagePollingUpdateHandler implements IUpdateHandler {

	MultiplayerActivity parent;
	ClientConnection connection;
	
	public MessagePollingUpdateHandler(MultiplayerActivity parent)
	{
		this.parent = parent;
		connection = parent.connection;
	}
	
	@Override
	public void onUpdate(float pSecondsElapsed) {
		JSONObject turn;
		
		try{
			if(connection.hasTurn())
			{
				turn = connection.getMessage();
				parent.recieveMessage(turn);
			}else if(parent.state == MultiplayerState.WAITING && !connection.listenerThread.isAlive())
			{
				connection.listenerRefresh();
			}
		}catch(NullPointerException e){
			connection.listenerRefresh();
		}
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub

	}

}
